﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using com.loto.Common;
using System.Text.RegularExpressions;

namespace com.huigo.Admin.Web.insurer
{
    public partial class AddIns : BasePage
    {
        private bool isModify
        {
            get
            {
                return sid == long.MinValue ? flgModify.Text == "0" ? false : true : true;
            }
        }

        private long sid
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["sid"]))
                {
                    return long.MinValue;
                }
                else
                {
                    long s = long.MinValue;
                    if (long.TryParse(Request.QueryString["sid"], out s))
                        flgModify.Text = "1";
                    return s;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (isModify)
                {
                    LoadData();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
           
            if (!VerifyField(FieldType.STRING, insName.Text, true))
            {
                MessageBox.Show(this.Page, "保险公司名称不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, insMeno.Text, true))
            {
                MessageBox.Show(this.Page, "保险公司简称不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, insLogo.Text, true))
            {
                MessageBox.Show(this.Page, "保险公司LOGO不能为空！");
                return;
            }
            else
            {
                string sSuffix = insLogo.Text.Substring(insLogo.Text.Trim().LastIndexOf('.') + 1);
                if (sSuffix.ToLower() != "jpg" && sSuffix.ToLower() != "jpeg" && sSuffix.ToLower() != "gif"
                 && sSuffix.ToLower() != "png" && sSuffix.ToLower() != "bmp")
                {
                    MessageBox.Show(this.Page, "请输入正确的保险公司LOGO！");
                    return;
                }
            }
            if (!VerifyField(FieldType.STRING, insWebSite.Text, true))
            {
                MessageBox.Show(this.Page, "保险公司网址不能为空！");
                return;
            }
            else
            {
                string s = @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
                Regex reg = new Regex(s);
                Match mch = reg.Match(insWebSite.Text);
                if (!mch.Success)
                {
                    MessageBox.Show(this.Page, "请输入正确的保险公司网址！");
                    return;
                }
            }
            if (!VerifyField(FieldType.STRING, insEx.Text, true))
            {
                MessageBox.Show(this.Page, "订单号前缀不能为空！");
                return;
            }
            else
            {
                if (insEx.Text.Length < 4)
                {
                    MessageBox.Show(this.Page, "订单号前缀为4位！");
                    return;
                }
            }
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            BaseService baseService = new BaseService();
            ParserXml parser;
            if (isModify)
            {
                argList.Add(new RequestFuncParams("insName", insName.Text));
                argList.Add(new RequestFuncParams("insMeno", insMeno.Text));
                argList.Add(new RequestFuncParams("insLogo", insLogo.Text));
                argList.Add(new RequestFuncParams("insEX", insEx.Text.ToUpper()));
                argList.Add(new RequestFuncParams("insWebSite", insWebSite.Text));
                argList.Add(new RequestFuncParams("insStatus", rdoStatus.SelectedValue));
                argList.Add(new RequestFuncParams("sid", sid.ToString()));
                if (CheckData())
                {
                    MessageBox.Show(this.Page, "保险公司名称和保险公司简称已存在！");
                    return;
                }
                else
                {
                    parser = baseService.RequestEntryService(Dict.ERequestFunc.UPDATE_INS_COMPANY, argList);
                }
            }
            else
            {
                argList.Add(new RequestFuncParams("insName", insName.Text));
                argList.Add(new RequestFuncParams("insShortName", insMeno.Text));
                argList.Add(new RequestFuncParams("insLogo", insLogo.Text));
                argList.Add(new RequestFuncParams("insEX", insEx.Text.ToUpper()));
                argList.Add(new RequestFuncParams("insWebSite", insWebSite.Text));
                argList.Add(new RequestFuncParams("insStatus", rdoStatus.SelectedValue));
                if (CheckData())
                {
                    MessageBox.Show(this.Page, "保险公司名称和保险公司简称和订单号前缀已存在！");
                    return;
                }
                else
                {
                    parser = baseService.RequestEntryService(Dict.ERequestFunc.ADD_INS_COMPANY, argList);
                }
            }
            if (parser != null)
                GoTo(GoToType.ADDSUC);
        }
        private bool CheckData()
        {
            List<RequestFuncParams> arglist = new List<RequestFuncParams>();
            arglist.Add(new RequestFuncParams("insName", insName.Text));
            arglist.Add(new RequestFuncParams("insMeno",insMeno.Text));
            arglist.Add(new RequestFuncParams("insEX",insEx.Text));
            arglist.Add(new RequestFuncParams("companyID", sid.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_INS_COMPANY_LIST, arglist);
            if (parser != null && parser.RowsCount > 0 && parser["returnargs", 0] == "true")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private void LoadData()
        {
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("sid",sid.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_INS_CONPANY_BY_ID,argList);
            if (parser != null && parser.RowsCount > 0)
            {
               string companyName = parser["CompanyName", 0].ToString();
               string compantShortName = parser["CompanyShortName", 0].ToString();
                insName.Text = companyName;
                insMeno.Text = compantShortName;
                insLogo.Text = parser["CompanyLogo", 0].ToString();
                insWebSite.Text = parser["CompanyWebsite", 0].ToString();
                insEx.Text = parser["CompanyEx", 0].ToString();
                rdoStatus.SelectedValue = parser["CompanyStatus", 0].ToString() ;
                insEx.Enabled = false;
            }
        }
    }
}
